package io.github.biezhi.elves.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.github.biezhi.elves.bidding.model.BiddingDO;
import io.github.biezhi.elves.bidding.model.BiddingDTO;
import io.github.biezhi.elves.bidding.model.BiddingKeywordDTO;
import io.github.biezhi.elves.bidding.model.BiddingListDO;
import io.github.biezhi.elves.bidding.model.BiddingListDTO;
import io.github.biezhi.elves.bidding.model.ChangeNoticeDTO;
import io.github.biezhi.elves.bidding.model.DownloadBiddingHtmlDO;
import io.github.biezhi.elves.bidding.model.DownloadBiddingHtmlDTO;
import io.github.biezhi.elves.bidding.model.NoticeTypeDTO;
import io.github.biezhi.elves.bidding.model.PurchaseModeDTO;
import io.github.biezhi.elves.bidding.model.RecordBiddingNoticeDO;
import io.github.biezhi.elves.bidding.model.RecordBiddingNoticeDTO;
import io.github.biezhi.elves.bidding.model.BiddingListQuery;
import io.github.biezhi.elves.bidding.model.RecordNoticeQuery;
import io.github.biezhi.elves.bidding.model.ResolveBiddingDO;
import io.github.biezhi.elves.bidding.model.ResolveBiddingDTO;
import io.github.biezhi.elves.bidding.model.ResolveBiddingQuery;
import io.github.biezhi.elves.bidding.model.WinBiddingDO;
import io.github.biezhi.elves.bidding.model.WinBiddingDTO;
import io.github.biezhi.elves.mapper.BidMapper;
import io.github.biezhi.elves.model.BiddingQuery;
import io.github.biezhi.elves.service.BidService;
import io.github.biezhi.elves.utils.ObjectUtils;

/**
 * 投标管理模块service组件
 *
 */
@Service("bidService")
public class BidServiceImpl implements BidService {
	/**
	 * 投标管理模块mapper组件
	 */
	@Autowired
	private BidMapper bidMapper;
	
	/***************************************新增操作 开始**********************************************************************/
	/**
	 * 新增发标公告数据
	 * @param	bidNotice	参数说明：发标公告DTO对象
	 */
	@Override
	public Boolean saveBidding(BiddingDTO bidding)  throws Exception {
		try {
			this.bidMapper.saveBidding(bidding.clone(BiddingDO.class));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 新增中标公告数据
	 * @param	winBidding	参数说明：中标公告DTO对象
	 */
	@Override
	public Boolean saveWinBidding(WinBiddingDTO winBidding)  throws Exception {
		try {
			this.bidMapper.saveWinBidding(winBidding.clone(WinBiddingDO.class));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 新增记录投标网公告标题数据
	 * @param	recordBiddingNotice	参数说明：投标网公告标题DTO对象
	 */
	@Override
	public Boolean saveRecordBiddingNotice(RecordBiddingNoticeDTO recordBiddingNotice)  throws Exception {
		try {
			this.bidMapper.saveRecordBiddingNotice(recordBiddingNotice.clone(RecordBiddingNoticeDO.class));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 新增下载投标网html数据
	 * @param	recordBiddingNotice	参数说明：投标网公告标题DTO对象
	 */
	@Override
	public Boolean saveDownloadBiddingHtml(DownloadBiddingHtmlDTO downloadBiddingHtml)  throws Exception {
		try {
			this.bidMapper.saveDownloadBiddingHtml(downloadBiddingHtml.clone(DownloadBiddingHtmlDO.class));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 新增公告清单存量数据(重复)
	 * @param	bidding		参数说明：公告清单DO对象
	 */
	@Override
	public Boolean saveBiddingStockRepeat(BiddingListDTO bidding)  throws Exception {
		try {
			this.bidMapper.saveBiddingStockRepeat(bidding.clone(BiddingListDO.class));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 新增存量公告清单数据
	 * @param	biddingStock		参数说明：存量公告清单DO对象
	 */
	@Override
	public Boolean saveResolveBiddingStock(ResolveBiddingDTO biddingStock)  throws Exception {
		try {
			this.bidMapper.saveResolveBiddingStock(biddingStock.clone(ResolveBiddingDO.class));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 新增处理解析公告清单数据
	 * @param	bidding		参数说明：公告清单DTO对象
	 */
	@Override
	public Boolean saveResolveBiddingList(BiddingListDTO bidding)  throws Exception {
		try {
			this.bidMapper.saveResolveBiddingList(bidding.clone(BiddingListDO.class));
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Boolean saveChangeNotice(ChangeNoticeDTO changeNotice) {
		try {
			this.bidMapper.saveChangeNotice(changeNotice);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/***************************************新增操作 结束**********************************************************************/
	
	/***************************************更新操作 开始**********************************************************************/
	/**
	 * 更新记录投标网公告标题数据
	 * @param	id	参数说明：取的是网站上的标题链接的后缀作为标题id
	 * @return	处理结果
	 * @throws Exception
	 */
	@Override
	public Boolean updateRecordBiddingNotice(String operationMode, Long id)  throws Exception {
		try {
			this.bidMapper.updateRecordBiddingNotice(operationMode, id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 更新存量公告清单数据表保存标识
	 * @param tableName		参数说明：表名
	 * @param save				参数说明：保存标识
	 * @param id					参数说明：id
	 */
	@Override
	public void updateBiddingListStock(String tableName, Integer save, Long id) throws Exception {
		this.bidMapper.updateBiddingListStock(tableName, save, id);
	}
	
	/**
	 * 更新公告数据表为空的公告标题数据
	 * @param prjId		参数说明：项目编号
	 */
	@Override
	public void updateBiddingStockImport(String noticeTitle, String prjId) throws Exception {
		this.bidMapper.updateBiddingStockImport(noticeTitle, prjId);
	}
	
	/**
	 * 更新公告数据表htmlContent数据
	 * @param downloadBiddingHtml		参数说明：下载对象
	 */
	@Override
	public void updateDownloadBiddingForHtmlContent(DownloadBiddingHtmlDTO downloadBiddingHtml) throws Exception {
		this.bidMapper.updateDownloadBiddingForHtmlContent(downloadBiddingHtml.clone(DownloadBiddingHtmlDO.class));
	}
	
	/**
	 * 更新公告数据表iframeHtmlContent数据
	 * @param downloadBiddingHtml		参数说明：下载对象
	 */
	@Override
	public void updateDownloadBiddingForIframeHtmlContent(DownloadBiddingHtmlDTO downloadBiddingHtml) throws Exception {
		this.bidMapper.updateDownloadBiddingForIframeHtmlContent(downloadBiddingHtml.clone(DownloadBiddingHtmlDO.class));
	}
	/***************************************更新操作 结束**********************************************************************/
	
	/***************************************查询操作 开始**********************************************************************/
	/**
	 * 统计未解析投标网公告标题数据
	 * @return	统计未解析投标网公告标题数据
	 */
	@Override
	public Integer countRecordBiddingNotice()  throws Exception {
		return this.bidMapper.countRecordBiddingNotice();
	}
	
	/**
	 * 根据记录id和标题查询记录表中是否存在公告标题id
	 * @param query
	 * @return	记录表中是否存在公告标题id
	 */
	@Override
	public Integer countRecordBiddingNoticeById(BiddingQuery query) throws Exception {
		return this.bidMapper.countRecordBiddingNoticeById(query);
	}
	
	/**
	 * 根据标题查询记录表中是否存在公告标题id
	 * @param query
	 * @return	记录表中是否存在公告标题id
	 */
	@Override
	public Integer countRecordBiddingNoticeByTitle(BiddingQuery query) throws Exception {
		return this.bidMapper.countRecordBiddingNoticeByTitle(query);
	}
	
	/**
	 * 根据记录id、公告类型、采购方式、关键字查询记录表中是否存在公告标题id
	 * @param query
	 * @return	记录表中是否存在公告标题id
	 */
	@Override
	public Integer countRecordBiddingNoticeByQuery(BiddingQuery query) throws Exception {
		return this.bidMapper.countRecordBiddingNoticeById(query);
	}
	
	/**
	 * 根据记录id查询下载html数据表中是否存在公告标题id
	 * @param query
	 * @return	下载html数据表中是否存在公告标题id
	 */
	@Override
	public Integer countDownloadBiddingHtmlById(BiddingQuery query) throws Exception {
		return this.bidMapper.countDownloadBiddingHtmlById(query);
	}
	
	/**
	 * 根据记录id、公告类型、采购方式、关键字查询下载html数据表中是否存在公告标题id
	 * @param query
	 * @return	下载html数据表中是否存在公告标题id
	 */
	@Override
	public Integer countDownloadBiddingHtmlByQuery(BiddingQuery query) throws Exception {
		return this.bidMapper.countDownloadBiddingHtmlById(query);
	}
	
	/**
	 * 根据表名和关键字统计指定表数据是否存在表中
	 * @param	tableName	参数说明：表名
	 * @param	keyword		参数说明：关键字
	 * @param	noticeType	参数说明：公告类型
	 * @return	统计指定表数据是否存在表中
	 */
	public Integer countByKeyword(String tableName, String keyword, String noticeType) throws Exception {
		return this.bidMapper.countByKeyword(tableName, keyword, noticeType);
	}
	
	/**
	 * 根据项目编号、公告标题、供应商和中标金额统计是否已存在解析过的数据
	 * @param query
	 * @return
	 */
	public Integer countResolveHtml(BiddingListQuery query) throws Exception {
		return this.bidMapper.countResolveHtml(query);
	}
	
	/**
	 * 根据项目编号、供应商和中标金额统计过滤数据重复
	 * @param query
	 * @return
	 */
	public Integer countResolveFilterRepeat(BiddingListQuery query) throws Exception {
		return this.bidMapper.countResolveFilterRepeat(query);
	}
	
	/**
	 * 查询初始化公告标题数据
	 * @param	query	参数说明：查询条件
	 * @return	公告标题数据集合
	 */
	@Override
	public List<RecordBiddingNoticeDTO> listRecordBiddingNotice(RecordNoticeQuery query) throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listRecordBiddingNotice(query), RecordBiddingNoticeDTO.class);
	}
	
	/**
	 * 查询公告详情页面数据
	 * @return	公告详情页面数据集合
	 */
	@Override
	public List<DownloadBiddingHtmlDTO> listDownloadBiddingHtml() throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listDownloadBiddingHtml(), DownloadBiddingHtmlDTO.class);
	}
	
	/**
	 * 查询下载公告html为空的数据
	 * @return
	 */
	public List<DownloadBiddingHtmlDTO> listDownloadBiddingHtmlIsNullHtmlContent() throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listDownloadBiddingHtmlIsNullHtmlContent(), DownloadBiddingHtmlDTO.class);
	}
	
	/**
	 * 查询下载公告iframe的html为空的数据
	 * @return
	 */
	public List<DownloadBiddingHtmlDTO> listDownloadBiddingHtmlIsNullIframeHtmlContent() throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listDownloadBiddingHtmlIsNullIframeHtmlContent(), DownloadBiddingHtmlDTO.class);
	}
	
	/**
	 * 根据记录ID查询公告详情页面数据
	 * @param	recordId	参数说明：记录ID
	 * @return	公告详情页面数据
	 */
	@Override
	public DownloadBiddingHtmlDTO downloadBiddingHtmlByRecordId(String recordId) throws Exception {
		return this.bidMapper.downloadBiddingHtmlByRecordId(recordId).clone(DownloadBiddingHtmlDTO.class);
	}
	
	/**
	 * 
	 */
	@Override
	public List<DownloadBiddingHtmlDTO> downloadBiddingHtmlByNoticeType(String noticeType) {
		try {
			return ObjectUtils.convertList(this.bidMapper.downloadBiddingHtmlByNoticeType(noticeType), DownloadBiddingHtmlDTO.class);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 查询公告类型数据
	 * @return	公告类型集合
	 */
	@Override
	public List<NoticeTypeDTO> listNoticeType() throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listNoticeType(), NoticeTypeDTO.class);
	}
	
	/**
	 * 查询采购方式数据
	 * @param noticeId	参数说明：公告ID
	 * @return	采购方式数据集合
	 */
	@Override
	public List<PurchaseModeDTO> listPurchaseMode(String noticeId) throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listPurchaseMode(noticeId), PurchaseModeDTO.class);
	}
	
	/**
	 * 查询关键字数据
	 * @return	关键字数据集合
	 */
	@Override
	public List<BiddingKeywordDTO> listBiddingKeyword(Integer keywordType) throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listBiddingKeyword(keywordType), BiddingKeywordDTO.class);
	}
	
	/**
	 * 统计存量重复公告清单标识为未保存存量表(t_bidding_stock)的数据
	 * @param siteName
	 * @return
	 */
	public Integer countResolveBiddingStockRepeat(String siteName) {
		return this.bidMapper.countResolveBiddingStockRepeat(siteName);
	}
	
	/**
	 * 公告清单存量数据重复
	 * @return
	 * throws Exception
	 */
	@Override
	public List<ResolveBiddingDTO> listResolveBiddingStockRepeatPage(ResolveBiddingQuery query) throws Exception {
		try {
			return ObjectUtils.convertList(this.bidMapper.listResolveBiddingStockRepeatPage(query), ResolveBiddingDTO.class);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 公告清单存量数据
	 * @param query		参数说明：查询条件
	 * @return
	 * throws Exception
	 */
	@Override
	public List<ResolveBiddingDTO> listResolveBiddingStockPage(ResolveBiddingQuery query) throws Exception {
		try {
			return ObjectUtils.convertList(this.bidMapper.listResolveBiddingStockPage(query), ResolveBiddingDTO.class);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 公告清单存量数据导入
	 * @return
	 * throws Exception
	 */
	@Override
	public List<BiddingListDTO> listBiddingStockImportPage() throws Exception {
		return ObjectUtils.convertList(this.bidMapper.listBiddingStockImportPage(), BiddingListDTO.class);
	}

	/**
	 * 根据网站名称查询最后一次爬取的公告发布时间
	 * @param siteName		参数说明：网站名称
	 * @param keyword			参数说明：网站名称
	 * @return
	 */
	@Override
	public RecordBiddingNoticeDTO getNewPubdateBySiteName(String siteName, String keyword) throws Exception {
		return this.bidMapper.getNewPubdateBySiteName(siteName, keyword).clone(RecordBiddingNoticeDTO.class);
	}
	
	/**
	 * 根据关键字查询信息
	 * @param keyword		参数说明：关键字
	 * @return	关键字数据
	 */
	@Override
	public BiddingKeywordDTO getBiddingKeywordByKeyword(String keyword) throws Exception {
		return this.bidMapper.getBiddingKeywordByKeyword(keyword).clone(BiddingKeywordDTO.class);
	}
	/***************************************查询操作 结束**********************************************************************/

}